package org.wekit.engine.pvm.process;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.wekit.engine.pvm.PvmActivity;
import org.wekit.engine.pvm.PvmException;
import org.wekit.engine.pvm.PvmTransition;
import org.wekit.engine.pvm.delegate.ActivityBehavior;

/**
 * 流程节点实体定义
 * @author HuangWeili
 *
 */
public class Activity extends Scope implements PvmActivity{

	private static final long serialVersionUID = -8711333606134894154L;

	protected List<Transition> outgoingTransitions=new ArrayList<Transition>();
	
	protected List<Transition> incomingTransitions=new ArrayList<Transition>();

	protected Map<String, Transition> namedOutgoingTransitions=new HashMap<String, Transition>();
	
	protected ActivityBehavior activityBehavior;
	
	//定义父类型
	protected Scope parent;
    //定义范围
	protected boolean isScope;
	
	
	public Activity(String id, ProcessDefinition processDefinition) {
		super(id, processDefinition);
	}

	/**
	 * 创建节点的出路由
	 * @return
	 */
	public Transition createOutgoingTransition(){
		return this.createOutgoingTransition(null);
	}
	
	public Transition createOutgoingTransition(String transitionId){
		Transition transition=new Transition(transitionId, this.processDefinition);
		transition.setSource(this);
		outgoingTransitions.add(transition);
		if(transitionId!=null)
		{
			if(namedOutgoingTransitions.containsKey(transitionId))
			{
				throw new PvmException("activity id="+this.id+" has duplicate transition "+transitionId);
			}
			namedOutgoingTransitions.put(transitionId, transition);
		}
		return transition;
	}
	
	/**
	 * 设置父关系
	 * @return
	 */
	public void setParent(Scope parent)
	{
		this.parent=parent;
	}
	
	@Override
	public Scope getParent() {
	  return this.parent;
	}
	
	/**
	 * 获取节点信息
	 * @return
	 */
	public Activity getParentActivity()
	{
		if(parent instanceof Activity)
		{
		  return (Activity)parent;
		}
		return null;
	}
	
	

	public void setIncomingTransitions(List<Transition> incomingTransitions){
		this.incomingTransitions=incomingTransitions;
	}
	
	@Override
	public List<Transition> getIncomingTransitions() {
		// TODO Auto-generated method stub
		return this.incomingTransitions;
	}

	/**
	 * 设置路由出口
	 * @param outgoingTransitions
	 */
	public void setOutgoingTransitions(List<Transition> outgoingTransitions){
		this.outgoingTransitions=outgoingTransitions;
	}
	
	@Override
	public List<Transition> getOutgoingTransitions() {
		
		return this.outgoingTransitions;
	}

	@Override
	public PvmTransition findOutgoingTransition(String transitionId) {
		return namedOutgoingTransitions.get(transitionId);
	}
	
	public String toString()
	{
		return "Activity("+this.id+")";
	}

	public ActivityBehavior getActivityBehavior() {
		return activityBehavior;
	}

	public void setActivityBehavior(ActivityBehavior activityBehavior) {
		this.activityBehavior = activityBehavior;
	}

	public boolean isScope() {
		return isScope;
	}

	public void setScope(boolean isScope) {
		this.isScope = isScope;
	}
	
}
